package com.systemic.controller;

import com.systemic.core.ResponseResult;
import com.systemic.pojo.MindfulnessClass;
import com.systemic.pojo.MindfulnessMedia;
import com.systemic.service.MindfulnessMediaService;
import com.systemic.vo.PageVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: MindfulnessMediaController
 * @Description:
 * @Author:
 * @CreateTime: 2024-01-26 10:00
 * @Version:
 **/
@RestController
@RequestMapping("/mindfulnessMedia")
public class MindfulnessMediaController {

    @Resource
    private MindfulnessMediaService service;


    /**
     * 新增
     * @param author
     * @param image
     * @param media
     * @param mediaName
     * @param mediaInfo
     * @param mediaClass
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<String> add(@RequestParam("author") Integer author,
                                      @RequestParam("image") MultipartFile image,
                                      @RequestParam("media") MultipartFile media,
                                      @RequestParam("mediaName") String mediaName,
                                      @RequestParam("mediaInfo") String mediaInfo,
                                      @RequestParam("mediaClass") Integer[] mediaClass,
                                      @RequestParam("isPublic") Integer isPublic){

        MindfulnessMedia mindfulnessMedia = new MindfulnessMedia();
        mindfulnessMedia.setAuthor(author);
        mindfulnessMedia.setMediaName(mediaName);
        mindfulnessMedia.setMediaInfo(mediaInfo);
        mindfulnessMedia.setIsPublic(isPublic);
        int row = service.add(image, media, mindfulnessMedia, mediaClass);
        return row!=0 ? ResponseResult.success("新增成功") : ResponseResult.failed("新增失败");
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public ResponseResult<String> delete(@PathVariable("id") Integer id){
        int row = service.delete(id);
        return row!=0 ? ResponseResult.success("删除成功") : ResponseResult.failed("删除失败");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @GetMapping("/deleteList/{ids}")
    public ResponseResult<String> deleteList(@PathVariable("ids") Integer[] ids){
        int row = service.deleteList(ids);
        return row!=0 ? ResponseResult.success("删除成功") : ResponseResult.failed("删除失败");
    }

    /**
     * 更新
     * @param id
     * @param media
     * @param image
     * @param mediaName
     * @param mediaInfo
     * @param mediaClass
     * @return
     */
    @PostMapping("/update")
    public ResponseResult<String> update(@RequestParam("id") Integer id,
                                         @RequestParam("media") MultipartFile media,
                                         @RequestParam("image") MultipartFile image,
                                         @RequestParam("mediaName") String mediaName,
                                         @RequestParam("mediaInfo") String mediaInfo,
                                         @RequestParam("mediaClass") Integer[] mediaClass,
                                         @RequestParam("isPublic") Integer isPublic){


        MindfulnessMedia mindfulnessMedia = new MindfulnessMedia();
        mindfulnessMedia.setId(id);
        mindfulnessMedia.setMediaName(mediaName);
        mindfulnessMedia.setMediaInfo(mediaInfo);
        mindfulnessMedia.setIsPublic(isPublic);
        int row = service.update(media, image, mindfulnessMedia, mediaClass);
        return row!=0 ? ResponseResult.success("修改成功") : ResponseResult.failed("修改失败");
    }

    /**
     * 根据id查询具体信息
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public ResponseResult<MindfulnessMedia> selectById(@PathVariable("id") Integer id){
        MindfulnessMedia res = service.selectById(id);
        return res!=null ? ResponseResult.success(res) : ResponseResult.failed();
    }


    /**
     * 根据name查询
     * @param mediaName
     * @return
     */
    @GetMapping("/likeSelect/{mediaName}")
    public ResponseResult<List<MindfulnessMedia>> likeSelect(@PathVariable("mediaName") String mediaName){
        List<MindfulnessMedia> res = service.likeSelect(mediaName);
        return res!=null ? ResponseResult.success(res) : ResponseResult.failed();
    }


    /**
     * 分页查询
     * @param pageVo
     * @return
     */
    @PostMapping("/likeSelectPage")
    public ResponseResult<List<MindfulnessMedia>> likeSelectPage(@RequestBody PageVo pageVo){
        List<MindfulnessMedia> res = service.likeSelectPage(pageVo);
        return !res.isEmpty() ? ResponseResult.success(res) : ResponseResult.failed();
    }


    /**
     * 分页查询 (管理端)
     * @param pageVo
     * @return
     */
    @PostMapping("/likeSelectList")
    public ResponseResult<List<MindfulnessMedia>> likeSelectList(@RequestBody PageVo pageVo){
        List<MindfulnessMedia> res = service.likeSelectList(pageVo);
        return !res.isEmpty() ? ResponseResult.success(res) : ResponseResult.failed();
    }

    /**
     * 分页查询（根据用户id）
     * @param pageVo
     * @return
     */
    @PostMapping("/selectByAuthor")
    public ResponseResult<List<MindfulnessMedia>> selectByAuthor(@RequestBody PageVo pageVo){
        List<MindfulnessMedia> res = service.selectByAuthor(pageVo);
        return !res.isEmpty() ? ResponseResult.success(res) : ResponseResult.failed();
    }



}
